

//
//  WQProgressView.swift
//  sanwuWeibo
//
//  Created by 王先强 on 15/12/19.
//  Copyright © 2015年 王先强. All rights reserved.
//

import UIKit

class WQProgressImageView: UIImageView {

    
    var progress: CGFloat = 0 {
        didSet {
            progressView.progress = progress
        }
    }
    
    init() {
        super.init(frame: CGRectZero)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    private func setupUI() {
        addSubview(progressView)
        progressView.backgroundColor = UIColor.clearColor()
        progressView.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(self.snp_edges)
        }
    }
    
    
    override func drawRect(rect: CGRect) {
        // Drawing code
    }

    private lazy var progressView: WQProgressView = WQProgressView()
}

private class WQProgressView: UIView {

    var progress: CGFloat = 0 {
        didSet {
            
            setNeedsDisplay()
        }
    }
    override func drawRect(rect: CGRect) {
        //创建绘制的圆相关参数
        let center = CGPointMake(rect.width * 0.5, rect.height * 0.5)
        let radius = min(rect.width, rect.height) * 0.5
        let startAngle = -M_PI_2
        let endAngle = CGFloat(startAngle) + CGFloat(M_PI) * 2.0 * CGFloat(progress)
        
        let path = UIBezierPath(arcCenter: center, radius: radius, startAngle: CGFloat(startAngle), endAngle: CGFloat(endAngle), clockwise: true)
        path.addLineToPoint(center)
        //先设置颜色在关闭path
        UIColor(white: 1.0, alpha: 0.4).setFill()
        path.closePath()
        path.fill()
    }

}

